package com.cn.intelligence.resetful.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.intelligence.pojo.Areas;
import com.cn.intelligence.pojo.Cities;
import com.cn.intelligence.pojo.Provinces;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbAddress;
import com.cn.intelligence.pojo.TbUser;
import com.cn.intelligence.resetful.pojo.Conts;
import com.cn.intelligence.resetful.service.UserOrderService;

/**
 * 获取用户信息
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/Api/User/")
public class UserOrderController {
	
	@Autowired
	public UserOrderService orderService;
    @Autowired  
    private HttpServletRequest request;  
	/**
	 * 获取用户订单信息
	 * @return
	 */
	@RequestMapping("getorder")
	@ResponseBody
	public TaotaoResult getorderInfo(Long userid, String orderStatus,int page){
		return TaotaoResult.ok(orderService.getOrderInfo(userid,orderStatus,page,page* 20));
	}
	/**
	 * 获取用户订单状态
	 * @return
	 */
	@RequestMapping("getorderSTatus")
	@ResponseBody
	public TaotaoResult getorderSTatus(Long userid){
		return TaotaoResult.ok(orderService.selectOrderSTatus(userid));
	}
	/**
	 * 获取用户收货地址
	 * @return
	 */
	@RequestMapping("getAddressInfo")
	@ResponseBody
	public TaotaoResult getAddressInfo(Long userid){
		return TaotaoResult.ok(orderService.getAddressInfo(userid));
	}
	/**
	 * 添加用户收货地址
	 * @return
	 */
	@RequestMapping("addAddressInfo")
	@ResponseBody
	public TaotaoResult addAddressInfo(TbAddress tbAddress){
		return TaotaoResult.ok(orderService.addAddressInfo(tbAddress));
	}
	/**
	 * 删除用户地址
	 * @return
	 */
	@RequestMapping("deleteAddress")
	@ResponseBody
	public TaotaoResult deleteAddress(String  userId,String addrId){
		return orderService.deleteAddress(userId,addrId);
	}
	/**
	 * 添加用户收货地址
	 * @return
	 */
	@RequestMapping("setDefultAddressInfo")
	@ResponseBody
	public TaotaoResult setDefultAddressInfo(TbAddress tbAddress) throws Exception{
		tbAddress.setIsDefault("1");
		return TaotaoResult.ok(orderService.updateAddressInfo(tbAddress));
	}
	/**
	 * 获取用户oppenid,如果没有的改用户则保存改用户信息
	 */
	@RequestMapping(value="getsessionkey",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult getsessionkey(String code){
		// 获取用户sessionID
		String sessionId = request.getSession().getId();
		
		return TaotaoResult.ok(orderService.getsessionkey(code,sessionId));
	}
	/**
	 * 验证用户信息，并且保存用户基本信息
	 */
	@RequestMapping(value="authlogin",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult authlogin(String SessionId,String gender,String NickName,String HeadUrl,String openid){
		TbUser tbuser = new TbUser();
		tbuser.setSessionid(SessionId);
		tbuser.setGender(Integer.parseInt(gender));
		tbuser.setNickname(NickName);
		tbuser.setHeadurl(HeadUrl);
		tbuser.setOpenid(openid);
		return TaotaoResult.ok(orderService.authlogin(tbuser));
	}
	/**
	 * 修改订单状态
	 */
	@RequestMapping(value="updateOrderStatus",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult updateOrderStatus(String orderId,String type,String remark){
		return TaotaoResult.ok(orderService.updateOrderStatus(orderId,type,remark));
	}
	/**
	 * 获取订单详情
	 */
	@RequestMapping(value="selectOrderDetail",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult selectOrderDetail(String orderId){
		return TaotaoResult.ok(orderService.selectOrderDetail(orderId));
	}
	
	/**
	 * 获取省份信息
	 * @param name
	 * @param id
	 * @return
	 */
	@RequestMapping("provinces")
	@ResponseBody
	public List<Provinces>  getProvinces(String name,String id){
		List<Provinces>  provinces = orderService.selectProvinces(name,id);
		return provinces;
	}
	/**
	 * 获取省份信息
	 * @param name
	 * @param id
	 * @return
	 */
	@RequestMapping("cities")
	@ResponseBody
	public List<Cities>  getCities(String name,String id){
		List<Cities>  cities = orderService.selectCities(name,id);
		return cities;
	}
	/**
	 * 获取省份信息
	 * @param name
	 * @param id
	 * @return
	 */
	@RequestMapping("areas")
	@ResponseBody
	public List<Areas>  getAreas(String name,String id){
		List<Areas>  areas = orderService.selectAreas(name,id);
		return areas;
	}
	/**
	 * 查询可支付订单
	 */
	@RequestMapping(value="selectPayOrder",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult selectPayOrder(String orderId,String userId,String openId){
        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }
		return TaotaoResult.ok(orderService.selectPayOrder(orderId,userId,openId,remoteAddr));
	}
	/**
	 * 查询可支付订单
	 */
	@RequestMapping(value="notifyPaySuccess/{orderId}",method=RequestMethod.GET)
	@ResponseBody
	public TaotaoResult notifyPaySuccess(@PathVariable String orderId){
		return TaotaoResult.ok(orderService.updateOrderStatus(orderId));
	}

    /**
     * 查询用户
     * @param TbUser
     * @return
     */
    @RequestMapping(value = "getUserByOpenId")
    @ResponseBody
    public TbUser getUserByOpenId(Long appId){
        TbUser euDataGridResult =orderService.getUserByOpenId(appId);
        return euDataGridResult;
    }
}
